package com.energy.analysis.weather.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.energy.analysis.comm.base.BaseMapper;
import com.energy.analysis.comm.base.BaseService;
import com.energy.analysis.comm.page.Page;
import com.energy.analysis.comm.utils.DateUtils;
import com.energy.analysis.comm.utils.HttpClientUtil;
import com.energy.analysis.comm.utils.StringUtils;
import com.energy.analysis.weather.entity.WeatherData;
import com.energy.analysis.weather.entity.WeatherDataStal;
import com.energy.analysis.weather.mapper.WeatherMapper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
@Service
public class WeatherService extends BaseService<WeatherData>{
	
	
	public final static String CITY_NO = "101050201";
	//中国气象网站 哈尔滨地址
	public final static String WEATHER_URL = "http://www.weather.com.cn/weather1d/101050101.shtml";
	//中国气象网站 哈尔滨地址7天
	public final static String WEATHER_URL_7DAY = "http://www.weather.com.cn/weather/101050101.shtml";
	
	
	//齐齐哈尔
	//qiqihaer
	//哈尔滨
	public final static String STA_ID = "haerbin";
	@Autowired
	private WeatherMapper weatherMapper;
	
	@Scheduled(cron = "0 0 0/1 * * ? ")//每一小时执行一次
	public Boolean apiData(){
		@SuppressWarnings("deprecation")
		//用了和风天气的免费api
		//https://www.heweather.com/documents/api/s6/weather  字段参考
		String url = "https://free-api.heweather.com/s6/weather?location="+STA_ID+"&key=f20bdb09fa004a70a4748f93a8ec1097";//齐齐哈尔
		String html = HttpClientUtil.httpGetRequest(url,null);
		
		//String hfWeather =  "{\"HeWeather6\":[{\"basic\":{\"cid\":\"CN101050201\",\"location\":\"齐齐哈尔\",\"parent_city\":\"齐齐哈尔\",\"admin_area\":\"黑龙江\",\"cnty\":\"中国\",\"lat\":\"47.34207916\",\"lon\":\"123.95791626\",\"tz\":\"+8.00\"},\"update\":{\"loc\":\"2018-04-16 15:47\",\"utc\":\"2018-04-16 07:47\"},\"status\":\"ok\",\"now\":{\"cloud\":\"70\",\"cond_code\":\"101\",\"cond_txt\":\"多云\",\"fl\":\"14\",\"hum\":\"24\",\"pcpn\":\"0.0\",\"pres\":\"1005\",\"tmp\":\"17\",\"vis\":\"20\",\"wind_deg\":\"171\",\"wind_dir\":\"南风\",\"wind_sc\":\"2\",\"wind_spd\":\"9\"},\"daily_forecast\":[{\"cond_code_d\":\"101\",\"cond_code_n\":\"101\",\"cond_txt_d\":\"多云\",\"cond_txt_n\":\"多云\",\"date\":\"2018-04-16\",\"hum\":\"37\",\"mr\":\"05:23\",\"ms\":\"18:39\",\"pcpn\":\"0.0\",\"pop\":\"0\",\"pres\":\"1010\",\"sr\":\"04:55\",\"ss\":\"18:34\",\"tmp_max\":\"18\",\"tmp_min\":\"6\",\"uv_index\":\"5\",\"vis\":\"20\",\"wind_deg\":\"5\",\"wind_dir\":\"北风\",\"wind_sc\":\"3-4\",\"wind_spd\":\"18\"},{\"cond_code_d\":\"100\",\"cond_code_n\":\"101\",\"cond_txt_d\":\"晴\",\"cond_txt_n\":\"多云\",\"date\":\"2018-04-17\",\"hum\":\"46\",\"mr\":\"05:52\",\"ms\":\"19:52\",\"pcpn\":\"0.0\",\"pop\":\"0\",\"pres\":\"1000\",\"sr\":\"04:53\",\"ss\":\"18:35\",\"tmp_max\":\"19\",\"tmp_min\":\"4\",\"uv_index\":\"5\",\"vis\":\"19\",\"wind_deg\":\"294\",\"wind_dir\":\"西北风\",\"wind_sc\":\"1-2\",\"wind_spd\":\"11\"},{\"cond_code_d\":\"101\",\"cond_code_n\":\"100\",\"cond_txt_d\":\"多云\",\"cond_txt_n\":\"晴\",\"date\":\"2018-04-18\",\"hum\":\"49\",\"mr\":\"06:24\",\"ms\":\"21:06\",\"pcpn\":\"0.0\",\"pop\":\"0\",\"pres\":\"1010\",\"sr\":\"04:51\",\"ss\":\"18:37\",\"tmp_max\":\"17\",\"tmp_min\":\"4\",\"uv_index\":\"2\",\"vis\":\"19\",\"wind_deg\":\"24\",\"wind_dir\":\"东北风\",\"wind_sc\":\"1-2\",\"wind_spd\":\"2\"}],\"lifestyle\":[{\"brf\":\"舒适\",\"txt\":\"白天不太热也不太冷，风力不大，相信您在这样的天气条件下，应会感到比较清爽和舒适。\",\"type\":\"comf\"},{\"brf\":\"较冷\",\"txt\":\"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。\",\"type\":\"drsg\"},{\"brf\":\"较易发\",\"txt\":\"昼夜温差较大，较易发生感冒，请适当增减衣服。体质较弱的朋友请注意防护。\",\"type\":\"flu\"},{\"brf\":\"较适宜\",\"txt\":\"天气较好，但因风力稍强，户外可选择对风力要求不高的运动，推荐您进行室内运动。\",\"type\":\"sport\"},{\"brf\":\"适宜\",\"txt\":\"天气较好，温度适宜，但风稍微有点大。这样的天气适宜旅游，您可以尽情地享受大自然的无限风光。\",\"type\":\"trav\"},{\"brf\":\"弱\",\"txt\":\"紫外线强度较弱，建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。\",\"type\":\"uv\"},{\"brf\":\"较适宜\",\"txt\":\"较适宜洗车，未来一天无雨，风力较小，擦洗一新的汽车至少能保持一天。\",\"type\":\"cw\"},{\"brf\":\"良\",\"txt\":\"气象条件有利于空气污染物稀释、扩散和清除，可在室外正常活动。\",\"type\":\"air\"}]}]}";
		JSONObject object = (JSONObject) JSON.parse(html);
		String heWeather6 = object.getString("HeWeather6");
		System.out.println(heWeather6);
		JSONArray arrayJsonObject = (JSONArray) JSON.parse(heWeather6);
		String jsonStr = arrayJsonObject.getJSONObject(0).toString();
		JSONObject weatherObject = (JSONObject) JSON.parse(jsonStr);
		
		String status = weatherObject.getString("status");
		if("ok".equals(status)) {
		
			String basicStr = weatherObject.getString("basic");//基础信息
			JSONObject basicObject = (JSONObject) JSON.parse(basicStr);
			
			String nowStr = weatherObject.getString("now");//实况天气
			JSONObject nowObject = (JSONObject) JSON.parse(nowStr);
			System.out.println(nowObject.toString());
			
			String dailyArrayStr = weatherObject.getString("daily_forecast");//天气预报
			JSONArray dailyArrayJsonObject = (JSONArray) JSON.parse(dailyArrayStr);
			String dailyArrayJsonStr = dailyArrayJsonObject.getJSONObject(0).toString();
			JSONObject dailyJsonObject =  (JSONObject) JSON.parse(dailyArrayJsonStr);  
			System.out.println(dailyJsonObject.toString());
			
			String prs = nowObject.getString("pres");//气压
			String prsSea = nowObject.getString("pres");//海平面气压
			String prsMax = nowObject.getString("pres");//最高气压
			String prsMin = nowObject.getString("pres");//最低气压
			String tem = nowObject.getString("tmp");//温度/气温
			String temMax = dailyJsonObject.getString("tmp_max");//最高气温
			String temMin = dailyJsonObject.getString("tmp_min");//最低气温
			String rhu = dailyJsonObject.getString("hum");//相对湿度
			String rhuMin = dailyJsonObject.getString("hum");//相对最小湿度
			String vap = nowObject.getString("pres");//水气压
			String pre1h = dailyJsonObject.getString("pcpn");//降水量
			String winDInstMax = nowObject.getString("wind_deg");//极大风速的风向(角度) 字符
			String winSMax = nowObject.getString("wind_spd");//最大风速 风速，公里/小时
			String winSInstMax = nowObject.getString("wind_spd");//极大风速 风速，公里/小时
			String winDSMax = nowObject.getString("wind_deg");//最大风速的风向(角度)  度
			String winSAvg10Mi = nowObject.getString("wind_spd");//10分钟平均风速风速，公里/小时
			String winDAvg10mi = nowObject.getString("wind_deg");//10分钟平均风向(角度)	度
				
			WeatherDataStal weatherDataStal = new WeatherDataStal();
			weatherDataStal.setStaId(STA_ID);
			//日期处理
			Calendar c = Calendar.getInstance();
	        String year = String.valueOf(c.get(Calendar.YEAR));//获取年份
	        String month=String.valueOf(c.get(Calendar.MONTH)+1);;//获取月份
	        String day=String.valueOf(c.get(Calendar.DATE));;//获取日
	        String minute=String.valueOf(c.get(Calendar.MINUTE));;//分
	        String hour=String.valueOf(c.get(Calendar.HOUR));;//小时
	        String second=String.valueOf(c.get(Calendar.SECOND));;//秒
	        String WeekOfYear = String.valueOf(c.get(Calendar.DAY_OF_WEEK));;//显示当前日期是一周的第几天，周一就是1，周五就是5
	        
	        if(month.length() < 2) {
	        		month = "0" + month;
	        }
	        if(day.length() < 2) {
	        		day = "0" + day;
	        }
	        
	        String date=year +""  + month + day;//格式：20160219
			weatherDataStal.setDay(date);
			weatherDataStal.setHour(hour);
			weatherDataStal.setPrs(new BigDecimal(prs));//气压
			weatherDataStal.setPrsSea(new BigDecimal(prsSea));//海平面气压
			weatherDataStal.setPrsMax(new BigDecimal(prsMax));//最高气压
			weatherDataStal.setPrsMin(new BigDecimal(prsMin));//最低气压
			weatherDataStal.setTem(new BigDecimal(tem));//温度/气温
			weatherDataStal.setTemMax(new BigDecimal(temMax));//最高气温
			weatherDataStal.setTemMin(new BigDecimal(temMin));//最低气温
			weatherDataStal.setRhu(new BigDecimal(rhu));//相对湿度
			weatherDataStal.setRhuMin(new BigDecimal(rhuMin));//相对最小湿度
			weatherDataStal.setVap(new BigDecimal(vap));//水气压
			weatherDataStal.setPre1h(new BigDecimal(pre1h));//降水量
			weatherDataStal.setWinDInstMax(new BigDecimal(winDInstMax));//极大风速的风向(角度) 字符
			weatherDataStal.setWinSMax(new BigDecimal(winSMax));//最大风速 米/秒
			weatherDataStal.setWinDSMax(new BigDecimal(winDSMax));//最大风速的风向(角度)  度
			weatherDataStal.setWinSInstMax(new BigDecimal(winSInstMax));//极大风速 米/秒
			weatherDataStal.setWinSAvg10Mi(new BigDecimal(winSAvg10Mi));//10分钟平均风速米/秒
			weatherDataStal.setWinDAvg10mi(new BigDecimal(winDAvg10mi));//10分钟平均风向(角度)	度
			weatherDataStal.setCrtDt(DateUtils.getYmdDate());
			weatherDataStal.setCrtTm(DateUtils.getHmsTime());
			weatherMapper.insertStal(weatherDataStal);
		}
		return true;
	}
	/**
	 * web检索获取
	 * @return
	 */
	public Boolean webSearchWeatherHome(){
		WeatherData weatherData = new WeatherData();
		String html = HttpClientUtil.httpGetRequest(WEATHER_URL,null);
		Document doc = Jsoup.parse(html);
		if(doc==null){
			return false;
		}
		Elements es = doc.select(".t");
		if(es == null || StringUtils.isEmpty(es.toString())){
			return false;
		}
		Element e = es.get(0);
		if(e == null){
			return false;
		}
		Elements tianqiqingkuang  = e.select(".wea");
		if(tianqiqingkuang == null || StringUtils.isEmpty(tianqiqingkuang.toString())){
			return false;
		}
		String baitian = tianqiqingkuang.get(0).text();
		String yewan = tianqiqingkuang.get(1).text();
		//获取温度
		Elements tem  = e.select(".tem").select("span");
		String teH = tem.get(0).text();
		String teL = tem.get(1).text();
		//获取风力
		Elements winWe  = e.select(".win").select("span");
		//风力
		String we1 = winWe.get(0).text();
		String we2 = winWe.get(1).text();
		//风向
		String wd1 = winWe.get(0).attr("title");
		String wd2 = winWe.get(1).attr("title");
		//图片位置
		Elements winImg  = e.select(".clearfix").select("li").select("big");
		String img1 = winImg.get(0).attr("class");
		String img2 =  winImg.get(1).attr("class");
		//获取24小时温度变化
		int idex = html.indexOf("class=\"winl\"");
		int idexEnd = html.indexOf("id=\"weatherChart\"");
		String str = html.substring(idex, idexEnd);
 
		//获取3小时温度变化
		String data3 = "";
		Pattern p3 = Pattern.compile("hour3data=([\\s\\S]*?)</script>");// 匹配<title>开头，</title>结尾的文档
		Matcher m3 = p3.matcher(str);// 开始编译
		while (m3.find()) {
			data3 = m3.group(1);
		}
		
		String day =DateUtils.getYmdDate();
		weatherData.setDay(day);
		weatherData.setTeC1(baitian);
		weatherData.setTeC2(yewan);
		weatherData.setTeH(Float.parseFloat(teH));
		weatherData.setTeL(Float.parseFloat(teL));
		weatherData.setTeWe1(we1);
		weatherData.setTeWe2(we2);
		weatherData.setTeWd1(wd1);
		weatherData.setTeWd2(wd2);
		weatherData.setTeI1(img1);
		weatherData.setTeI2(img2);
		weatherData.setHour3Data(data3);
		weatherData.setCityNo(CITY_NO);
		weatherData.setCrtDt(day);
		weatherData.setCrtTm(DateUtils.getHmsTime());
		logger.debug("数据采集完毕:" + weatherData.toString());
		return weatherMapper.insert(weatherData) > 0;
	}
	public boolean hday0(Elements tianqiqingkuang,ArrayList<Object> d7) {
		// 当天
		Elements imgs0 = tianqiqingkuang.get(0).select("big");
		String img0_1 = imgs0.get(0).attr("class").replace("png40 ", "");
		String img0_2 = imgs0.get(1).attr("class").replace("png40 ", "");
		Elements wea = tianqiqingkuang.get(0).select(".wea");
		String weaStr0 = wea.get(0).text();
		String wea0_1 = weaStr0;
		String wea0_2 = weaStr0;
		if (weaStr0.indexOf("转") > 0) {
			wea0_1 = weaStr0.substring(0, weaStr0.indexOf("转"));
			wea0_2 = weaStr0.substring(weaStr0.indexOf("转") + 1);
		}
		Elements wen0 = tianqiqingkuang.get(0).select(".tem");
		Float wen0_1 = Float.parseFloat(wen0.select("span").text().replace("℃", ""));
		Float wen0_2 = Float.parseFloat(wen0.select("i").text().replace("℃", ""));
		//平均溫度
		Float wenAvg = (wen0_1 +wen0_2) / 2;
		Elements feng0 = tianqiqingkuang.get(0).select(".win");
		String feng0_1 = feng0.select("span").get(0).attr("title");
		String feng0_2 = feng0_1;
		if (feng0.select("span").size() > 1) {
			feng0_2 = feng0.select("span").get(1).attr("title");
		}
		String jfeng0 = feng0.select("i").text();
		String jfeng0_1 = jfeng0;
		String jfeng0_2 = jfeng0;
		if (jfeng0.indexOf("转") > 0) {
			jfeng0_1 = jfeng0.substring(0, jfeng0.indexOf("转"));
			jfeng0_2 = jfeng0.substring(jfeng0.indexOf("转") + 1);
		}
		 //当天
		List<Object> l0 = (List<Object>) d7.get(0);
		List<Map<String,Object>> listD0  = new  ArrayList<Map<String,Object>>();
		for (int i = 0; i <l0.size() ; i++) {
			String[] ss = String.valueOf(l0.get(i)).split(",");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("d",ss[0] );
			map.put("i",ss[1] );
			map.put("y",ss[2] );
			map.put("w",ss[3].replace("℃", "") );
			map.put("f",ss[4] );
			map.put("j",ss[5] );
			listD0.add(map);
		}
		String jsonh3 = new Gson().toJson(listD0);
		WeatherData weatherData = new WeatherData();
		String day =DateUtils.getYmdDate();
		weatherData.setDay(day);
		weatherData.setTeC1(wea0_1);
		weatherData.setTeC2(wea0_2);
		weatherData.setTeH(wen0_1);
		weatherData.setTeL(wen0_2);
		weatherData.setTeA(wenAvg);
		weatherData.setTeWe1(jfeng0_1);
		weatherData.setTeWe2(jfeng0_2);
		weatherData.setTeWd1(feng0_1);
		weatherData.setTeWd2(feng0_2);
		weatherData.setTeI1(img0_1);
		weatherData.setTeI2(img0_2);
		weatherData.setHour3Data(jsonh3);
		weatherData.setCityNo(CITY_NO);
		weatherData.setCrtDt(day);
		weatherData.setCrtTm(DateUtils.getHmsTime());
		logger.debug("采集当天信息:" + weatherData.toString());
		if(weatherMapper.getWeatherByDay(day) > 0){ //查看是否存在今天的天气预报
			return weatherMapper.updateByDay(weatherData) > 0;
		}else{
			return weatherMapper.insert(weatherData) > 0;
		}
	}

	public  boolean hday1(Elements tianqiqingkuang,ArrayList<Object> d7) {
		// 后一天
		Elements imgs1 = tianqiqingkuang.get(1).select("big");
		String img1_1 = imgs1.get(0).attr("class").replace("png40 ", "");
		String img1_2 = imgs1.get(1).attr("class").replace("png40 ", "");
		
		Elements wea1 = tianqiqingkuang.get(1).select(".wea");
		String weaStr1 = wea1.get(0).text();
		String wea1_1 = weaStr1;
		String wea1_2 = weaStr1;
		if (weaStr1.indexOf("转") > 0) {
			wea1_1 = weaStr1.substring(0, weaStr1.indexOf("转"));
			wea1_2 = weaStr1.substring(weaStr1.indexOf("转") + 1);
		}
		Elements wen1 = tianqiqingkuang.get(1).select(".tem");
		Float wen1_1 = Float.parseFloat(wen1.select("span").text().replace("℃", ""));
		Float wen1_2 = Float.parseFloat(wen1.select("i").text().replace("℃", ""));
		//平均溫度
		Float wenAvg = (wen1_1 +wen1_2) / 2;
		Elements feng1 = tianqiqingkuang.get(1).select(".win");
		String feng1_1 = feng1.select("span").get(0).attr("title");
		String feng1_2 = feng1_1;
		if (feng1.select("span").size() > 1) {
			feng1_2 = feng1.select("span").get(1).attr("title");
		}
		String jfeng1 = feng1.select("i").text();
		String jfeng1_1 = jfeng1;
		String jfeng1_2 = jfeng1;
		if (jfeng1.indexOf("转") > 0) {
			jfeng1_1 = jfeng1.substring(0, jfeng1.indexOf("转"));
			jfeng1_2 = jfeng1.substring(jfeng1.indexOf("转") + 1);
		}
		List<Object> l1 = (List<Object>) d7.get(1);
		List<Map<String,Object>> listD0  = new  ArrayList<Map<String,Object>>();
		for (int i = 0; i <l1.size() ; i++) {
			String[] ss = String.valueOf(l1.get(i)).split(",");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("d",ss[0] );
			map.put("i",ss[1] );
			map.put("y",ss[2] );
			map.put("w",ss[3].replace("℃", "") );
			map.put("f",ss[4] );
			map.put("j",ss[5] );
			listD0.add(map);
		}
		String jsonh3 = new Gson().toJson(listD0);
		WeatherData weatherData = new WeatherData();
		//当前日期加1天
		String day = DateUtils.dayAdd(1,"yyyyMMdd");
		weatherData.setDay(day);
		weatherData.setTeC1(wea1_1);
		weatherData.setTeC2(wea1_2);
		weatherData.setTeH(wen1_1);
		weatherData.setTeL(wen1_2);
		weatherData.setTeA(wenAvg);
		weatherData.setTeWe1(jfeng1_1);
		weatherData.setTeWe2(jfeng1_2);
		weatherData.setTeWd1(feng1_1);
		weatherData.setTeWd2(feng1_2);
		weatherData.setTeI1(img1_1);
		weatherData.setTeI2(img1_2);
		weatherData.setHour3Data(jsonh3);
		weatherData.setCityNo(CITY_NO);
		weatherData.setCrtDt(DateUtils.getYmdDate());
		weatherData.setCrtTm(DateUtils.getHmsTime());
		logger.debug("采集后一天信息:" + weatherData.toString());
		if(weatherMapper.getWeatherByDay(day) > 0){ //查看是否存在天气预报
			return weatherMapper.updateByDay(weatherData) > 0;
		}else{
			return weatherMapper.insert(weatherData) > 0;
		}
	}

	public boolean hday2(Elements tianqiqingkuang,ArrayList<Object> d7) {
		// 后二天
		Elements imgs2 = tianqiqingkuang.get(2).select("big");
		String img2_1 = imgs2.get(0).attr("class").replace("png40 ", "");
		String img2_2 = imgs2.get(1).attr("class").replace("png40 ", "");
		Elements wea2 = tianqiqingkuang.get(2).select(".wea");
		String weaStr2 = wea2.get(0).text();
		String wea2_1 = weaStr2;
		String wea2_2 = weaStr2;
		if (weaStr2.indexOf("转") > 0) {
			wea2_1 = weaStr2.substring(0, weaStr2.indexOf("转"));
			wea2_2 = weaStr2.substring(weaStr2.indexOf("转") + 1);
		}
		Elements wen2 = tianqiqingkuang.get(2).select(".tem");
		Float wen2_1 = Float.parseFloat(wen2.select("span").text().replace("℃", ""));
		Float wen2_2 = Float.parseFloat(wen2.select("i").text().replace("℃", ""));
		//平均溫度
		Float wenAvg = (wen2_1 +wen2_2) / 2;
		Elements feng2 = tianqiqingkuang.get(2).select(".win");
		String feng2_1 = feng2.select("span").get(0).attr("title");
		String feng2_2 = feng2_1;
		if (feng2.select("span").size() > 1) {
			feng2_2 = feng2.select("span").get(1).attr("title");
		}
		String jfeng2 = feng2.select("i").text();
		String jfeng2_1 = jfeng2;
		String jfeng2_2 = jfeng2;
		if (jfeng2.indexOf("转") > 0) {
			jfeng2_1 = jfeng2.substring(0, jfeng2.indexOf("转"));
			jfeng2_2 = jfeng2.substring(jfeng2.indexOf("转") + 1);
		}
		List<Object> l2 = (List<Object>) d7.get(2);
		List<Map<String,Object>> listD0  = new  ArrayList<Map<String,Object>>();
		for (int i = 0; i <l2.size() ; i++) {
			String[] ss = String.valueOf(l2.get(i)).split(",");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("d",ss[0] );
			map.put("i",ss[1] );
			map.put("y",ss[2] );
			map.put("w",ss[3].replace("℃", "") );
			map.put("f",ss[4] );
			map.put("j",ss[5] );
			listD0.add(map);
		}
		String jsonh3 = new Gson().toJson(listD0);
		WeatherData weatherData = new WeatherData();
		//当前日期加2天
		String day = DateUtils.dayAdd(2,"yyyyMMdd");
		weatherData.setDay(day);
		weatherData.setTeC1(wea2_1);
		weatherData.setTeC2(wea2_2);
		weatherData.setTeH(wen2_1);
		weatherData.setTeL(wen2_2);
		weatherData.setTeA(wenAvg);
		weatherData.setTeWe1(jfeng2_1);
		weatherData.setTeWe2(jfeng2_2);
		weatherData.setTeWd1(feng2_1);
		weatherData.setTeWd2(feng2_2);
		weatherData.setTeI1(img2_1);
		weatherData.setTeI2(img2_2);
		weatherData.setHour3Data(jsonh3);
		weatherData.setCityNo(CITY_NO);
		weatherData.setCrtDt(DateUtils.getYmdDate());
		weatherData.setCrtTm(DateUtils.getHmsTime());
		logger.debug("采集后二天信息:" + weatherData.toString());
		if(weatherMapper.getWeatherByDay(day) > 0){ //查看是否存在天气预报
			return weatherMapper.updateByDay(weatherData) > 0;
		}else{
			return weatherMapper.insert(weatherData) > 0;
		}
	}

	public boolean hday3(Elements tianqiqingkuang,ArrayList<Object> d7) {
		// 后三天
		Elements imgs3 = tianqiqingkuang.get(3).select("big");
		String img3_1 = imgs3.get(0).attr("class").replace("png40 ", "");
		String img3_2 = imgs3.get(1).attr("class").replace("png40 ", "");
		Elements wea3 = tianqiqingkuang.get(3).select(".wea");
		String weaStr3 = wea3.get(0).text();
		String wea3_1 = weaStr3;
		String wea3_2 = weaStr3;
		if (weaStr3.indexOf("转") > 0) {
			wea3_1 = weaStr3.substring(0, weaStr3.indexOf("转"));
			wea3_2 = weaStr3.substring(weaStr3.indexOf("转") + 1);
		}
		Elements wen3 = tianqiqingkuang.get(3).select(".tem");
		Float wen3_1 = Float.parseFloat(wen3.select("span").text().replace("℃", ""));
		Float wen3_2 = Float.parseFloat(wen3.select("i").text().replace("℃", ""));
		//平均溫度
		Float wenAvg = (wen3_1 +wen3_2) / 2;
		Elements feng3 = tianqiqingkuang.get(3).select(".win");
		String feng3_1 = feng3.select("span").get(0).attr("title");
		String feng3_2 = feng3_1;
		if (feng3.select("span").size() > 1) {
			feng3_2 = feng3.select("span").get(1).attr("title");
		}
		String jfeng3 = feng3.select("i").text();
		String jfeng3_1 = jfeng3;
		String jfeng3_2 = jfeng3;
		if (jfeng3.indexOf("转") > 0) {
			jfeng3_1 = jfeng3.substring(0, jfeng3.indexOf("转"));
			jfeng3_2 = jfeng3.substring(jfeng3.indexOf("转") + 1);
		}
		 //后三天
		
		List<Object> l3 = (List<Object>) d7.get(3); 
		List<Map<String,Object>> listD3  = new  ArrayList<Map<String,Object>>();
		for (int i = 0; i <l3.size() ; i++) {
			String[] ss = String.valueOf(l3.get(i)).split(",");
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("d",ss[0] );
			map.put("i",ss[1] );
			map.put("y",ss[2] );
			String wendu = ss[3].replace("℃", "");
			map.put("w",wendu );
			map.put("f",ss[4] );
			map.put("j",ss[5] );
			listD3.add(map);
		}
		//处理后三天的时间不足问题
		if(l3.size() < 8 ){
			Map<String,Object> m0 = listD3.get(0);
			Map<String,Object> m1 = listD3.get(1);
			Map<String,Object> m2 = listD3.get(2);
			Map<String,Object> m3 = listD3.get(3);
			
			Map<String,Object> m0_1  = new HashMap<String,Object>();
			m0_1.putAll(m0);
			m0_1.put("d", ((String)m0_1.get("d")).replace("08", "11"));
			m0_1.put("w",(int)((Integer.parseInt((String)m0.get("w")) + Integer.parseInt((String)m1.get("w")))/2));
			
			Map<String,Object> m1_2  = new HashMap<String,Object>();
			m1_2.putAll(m1);
			m1_2.put("d", ((String)m1_2.get("d")).replace("14", "17"));
			m1_2.put("w",(int)((Integer.parseInt((String)m1.get("w")) + Integer.parseInt((String)m2.get("w")))/2));
			
			Map<String,Object> m2_3  = new HashMap<String,Object>();
			m2_3.putAll(m2);
			m2_3.put("d", ((String)m2_3.get("d")).replace("20", "23"));
			m2_3.put("w",(int)((Integer.parseInt((String)m2.get("w")) + Integer.parseInt((String)m3.get("w")))/2));
			
			Map<String,Object> m3_4  = new HashMap<String,Object>();
			m3_4.putAll(m3);
			m3_4.put("d", ((String)m3_4.get("d")).replace("20", "23"));
			m3_4.put("w",(int)((Integer.parseInt((String)m3.get("w"))) -1));
			
			listD3.add(1, m0_1);
			listD3.add(3, m1_2);
			listD3.add(5, m2_3);
			listD3.add(7, m3_4);
		}
		String jsonh3 = new Gson().toJson(listD3);
		WeatherData weatherData = new WeatherData();
		//当前日期加3天
		String day = DateUtils.dayAdd(3,"yyyyMMdd");
		weatherData.setDay(day);
		weatherData.setTeC1(wea3_1);
		weatherData.setTeC2(wea3_2);
		weatherData.setTeH(wen3_1);
		weatherData.setTeL(wen3_2);
		weatherData.setTeA(wenAvg);
		weatherData.setTeWe1(jfeng3_1);
		weatherData.setTeWe2(jfeng3_2);
		weatherData.setTeWd1(feng3_1);
		weatherData.setTeWd2(feng3_2);
		weatherData.setTeI1(img3_1);
		weatherData.setTeI2(img3_2);
		weatherData.setHour3Data(jsonh3);
		weatherData.setCityNo(CITY_NO);
		weatherData.setCrtDt(DateUtils.getYmdDate());
		weatherData.setCrtTm(DateUtils.getHmsTime());
		logger.debug("采集后二天信息:" + weatherData.toString());
		if(weatherMapper.getWeatherByDay(day) > 0){ //查看是否存在天气预报
			return weatherMapper.updateByDay(weatherData) > 0;
		}else{
			return weatherMapper.insert(weatherData) > 0;
		}
	}
	/**
	 * web检索获取
	 * @return
	 */
	@Scheduled(cron = "0 15 00/3 * * ? ")//每天00点15分触发,然后每3个小时执行
	public Boolean webSearchWeather(){
		
		WeatherData weatherData = new WeatherData();
		String html = HttpClientUtil.httpGetRequest(WEATHER_URL_7DAY, null);
		//获取3小时温度变化
		String data3 = "";
		Pattern p3 = Pattern.compile("hour3data=([\\s\\S]*?)</script>");// 匹配<title>开头，</title>结尾的文档
		Matcher m3 = p3.matcher(html);// 开始编译
		while (m3.find()) {
			data3 = m3.group(1);
		}
		Map<String,Object> bmap = new Gson().fromJson(data3, new TypeToken<Map<String, Object>>(){}.getType());
		ArrayList<Object> d7 = (ArrayList<Object>) bmap.get("7d");
		Document doc = Jsoup.parse(html);
		Elements es = doc.select("[class=t clearfix]");
		Element e = es.get(0);
		Elements tianqiqingkuang = e.select("li");
		try {
			hday0(tianqiqingkuang, d7);
		} catch (Exception ex) {
			logger.error("取当日温度信息错误");
			ex.printStackTrace();
		}
		try {
			hday1(tianqiqingkuang,d7);
		}catch (Exception ex) {
			logger.error("取后一日温度信息错误");	
			ex.printStackTrace();
		}	
		try {
			hday2(tianqiqingkuang,d7);
		}catch (Exception ex) {
			logger.error("取后二日温度信息错误");
			ex.printStackTrace();
		}
		try {
			hday3(tianqiqingkuang,d7);
		}catch (Exception ex) {
			logger.error("取后三日温度信息错误");
			ex.printStackTrace();
		}	
		return true;
	}
	/**
	 * 获取当前天气
	 * @return
	 */
	public WeatherData findWeatherCurr(String day){
		return weatherMapper.getWeatherDataByDay(day);
	}
	/**
	 * 获取当前实时天气
	 * @return
	 */
	public WeatherDataStal findWeatherStalCurr(){
		return weatherMapper.getWeatherDataStalByDay(DateUtils.getYmdDate());
	}
	@Override
	public BaseMapper<WeatherData> getMapper() {
		return weatherMapper;
	}
	/**
	 * 根据日期获取7天气数据,当天,后三天 前三天
	 * @return
	 */
	public List<WeatherData> getWeather7Day(){
		String day = DateUtils.getYmdDate();
		String befDay1 = DateUtils.dayAdd(-1,"yyyyMMdd");
		String befDay2 = DateUtils.dayAdd(-2,"yyyyMMdd");
		String befDay3 = DateUtils.dayAdd(-3,"yyyyMMdd");
		String aftDay1 = DateUtils.dayAdd(1,"yyyyMMdd");
		String aftDay2 = DateUtils.dayAdd(2,"yyyyMMdd");
		String aftDay3 = DateUtils.dayAdd(3,"yyyyMMdd");
		String[] days = new String[]{day,befDay1,befDay2,befDay3,aftDay1,aftDay2,aftDay3};
		return weatherMapper.getWeatherDay(days);
	}
	/**
	 * 获取平均温度列表
	 * @param page
	 * @return
	 */
	public List<WeatherData> getAvgTemList(Page<WeatherData> page){
		List<WeatherData> rows = weatherMapper.getAvgTemList(page);
		page.setRows(rows);
		return rows;
	}
	
	public int getAvgTemListCount(Page<WeatherData> page) {
		int count =  weatherMapper.getAvgTemListCount(page);
		page.setTotal(count);
		return count;
	}
	/**
	 * 获取4天详细信息
	 * @return
	 */
	public List<WeatherData> getDay4Data() {
		String day0 =DateUtils.getYmdDate();
		String day1 = DateUtils.dayAdd(1,"yyyyMMdd");
		String day2 = DateUtils.dayAdd(2,"yyyyMMdd");
		String day3 = DateUtils.dayAdd(3,"yyyyMMdd");
		String[] days = new String[]{day0,day1,day2,day3};
		return weatherMapper.getWeatherDay(days);
	}
	/**
	 * 根据日期获取气象站实时温度
	 * @param day
	 * @return
	 */
	public List<Map<String,Object>> getStalWeatherByDay(String day){
		return weatherMapper.getStalWeatherByDay(day);
	}
	
	
	public static void main(String[] args) {
		//和风天气
		String hfWeather =  "{\"HeWeather6\":[{\"basic\":{\"cid\":\"CN101050201\",\"location\":\"齐齐哈尔\",\"parent_city\":\"齐齐哈尔\",\"admin_area\":\"黑龙江\",\"cnty\":\"中国\",\"lat\":\"47.34207916\",\"lon\":\"123.95791626\",\"tz\":\"+8.00\"},\"update\":{\"loc\":\"2018-04-16 15:47\",\"utc\":\"2018-04-16 07:47\"},\"status\":\"ok\",\"now\":{\"cloud\":\"70\",\"cond_code\":\"101\",\"cond_txt\":\"多云\",\"fl\":\"14\",\"hum\":\"24\",\"pcpn\":\"0.0\",\"pres\":\"1005\",\"tmp\":\"17\",\"vis\":\"20\",\"wind_deg\":\"171\",\"wind_dir\":\"南风\",\"wind_sc\":\"2\",\"wind_spd\":\"9\"},\"daily_forecast\":[{\"cond_code_d\":\"101\",\"cond_code_n\":\"101\",\"cond_txt_d\":\"多云\",\"cond_txt_n\":\"多云\",\"date\":\"2018-04-16\",\"hum\":\"37\",\"mr\":\"05:23\",\"ms\":\"18:39\",\"pcpn\":\"0.0\",\"pop\":\"0\",\"pres\":\"1010\",\"sr\":\"04:55\",\"ss\":\"18:34\",\"tmp_max\":\"18\",\"tmp_min\":\"6\",\"uv_index\":\"5\",\"vis\":\"20\",\"wind_deg\":\"5\",\"wind_dir\":\"北风\",\"wind_sc\":\"3-4\",\"wind_spd\":\"18\"},{\"cond_code_d\":\"100\",\"cond_code_n\":\"101\",\"cond_txt_d\":\"晴\",\"cond_txt_n\":\"多云\",\"date\":\"2018-04-17\",\"hum\":\"46\",\"mr\":\"05:52\",\"ms\":\"19:52\",\"pcpn\":\"0.0\",\"pop\":\"0\",\"pres\":\"1000\",\"sr\":\"04:53\",\"ss\":\"18:35\",\"tmp_max\":\"19\",\"tmp_min\":\"4\",\"uv_index\":\"5\",\"vis\":\"19\",\"wind_deg\":\"294\",\"wind_dir\":\"西北风\",\"wind_sc\":\"1-2\",\"wind_spd\":\"11\"},{\"cond_code_d\":\"101\",\"cond_code_n\":\"100\",\"cond_txt_d\":\"多云\",\"cond_txt_n\":\"晴\",\"date\":\"2018-04-18\",\"hum\":\"49\",\"mr\":\"06:24\",\"ms\":\"21:06\",\"pcpn\":\"0.0\",\"pop\":\"0\",\"pres\":\"1010\",\"sr\":\"04:51\",\"ss\":\"18:37\",\"tmp_max\":\"17\",\"tmp_min\":\"4\",\"uv_index\":\"2\",\"vis\":\"19\",\"wind_deg\":\"24\",\"wind_dir\":\"东北风\",\"wind_sc\":\"1-2\",\"wind_spd\":\"2\"}],\"lifestyle\":[{\"brf\":\"舒适\",\"txt\":\"白天不太热也不太冷，风力不大，相信您在这样的天气条件下，应会感到比较清爽和舒适。\",\"type\":\"comf\"},{\"brf\":\"较冷\",\"txt\":\"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。\",\"type\":\"drsg\"},{\"brf\":\"较易发\",\"txt\":\"昼夜温差较大，较易发生感冒，请适当增减衣服。体质较弱的朋友请注意防护。\",\"type\":\"flu\"},{\"brf\":\"较适宜\",\"txt\":\"天气较好，但因风力稍强，户外可选择对风力要求不高的运动，推荐您进行室内运动。\",\"type\":\"sport\"},{\"brf\":\"适宜\",\"txt\":\"天气较好，温度适宜，但风稍微有点大。这样的天气适宜旅游，您可以尽情地享受大自然的无限风光。\",\"type\":\"trav\"},{\"brf\":\"弱\",\"txt\":\"紫外线强度较弱，建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。\",\"type\":\"uv\"},{\"brf\":\"较适宜\",\"txt\":\"较适宜洗车，未来一天无雨，风力较小，擦洗一新的汽车至少能保持一天。\",\"type\":\"cw\"},{\"brf\":\"良\",\"txt\":\"气象条件有利于空气污染物稀释、扩散和清除，可在室外正常活动。\",\"type\":\"air\"}]}]}";
		JSONObject object = (JSONObject) JSON.parse(hfWeather);
		String heWeather6 = object.getString("HeWeather6");
		System.out.println(heWeather6);
		JSONArray arrayJsonObject = (JSONArray) JSON.parse(heWeather6);
		String jsonStr = arrayJsonObject.getJSONObject(0).toString();
		JSONObject weatherObject = (JSONObject) JSON.parse(jsonStr);
		
		String status = weatherObject.getString("status");
		
		String basicStr = weatherObject.getString("basic");//基础信息
		JSONObject basicObject = (JSONObject) JSON.parse(basicStr);
		
		String nowStr = weatherObject.getString("now");//实况天气
		JSONObject nowObject = (JSONObject) JSON.parse(nowStr);
		System.out.println(nowObject.toString());
		
		String dailyArrayStr = weatherObject.getString("daily_forecast");//天气预报
		JSONArray dailyArrayJsonObject = (JSONArray) JSON.parse(dailyArrayStr);
		String dailyArrayJsonStr = dailyArrayJsonObject.getJSONObject(0).toString();
		JSONObject dailyJsonObject =  (JSONObject) JSON.parse(dailyArrayJsonStr);  
		System.out.println(dailyJsonObject.toString());
		
		String prs = nowObject.getString("pres");//气压
		String prsSea = nowObject.getString("pres");//海平面气压
		String prsMax = nowObject.getString("pres");//最高气压
		String prsMin = nowObject.getString("pres");//最低气压
		String tem = nowObject.getString("tmp");//温度/气温
		String temMax = dailyJsonObject.getString("tmp_max");//最高气温
		String temMin = dailyJsonObject.getString("tmp_min");//最低气温
		String rhu = dailyJsonObject.getString("hum");//相对湿度
		String rhuMin = dailyJsonObject.getString("hum");//相对最小湿度
		String vap = nowObject.getString("pres");//水气压
		String pre1h = dailyJsonObject.getString("pcpn");//降水量
		String winDInstMax = nowObject.getString("wind_deg");//极大风速的风向(角度) 字符
		String winSMax = nowObject.getString("wind_spd");//最大风速 风速，公里/小时
		String winSInstMax = nowObject.getString("wind_spd");//极大风速 风速，公里/小时
		String winDSMax = nowObject.getString("wind_deg");//最大风速的风向(角度)  度
		String winSAvg10Mi = nowObject.getString("wind_spd");//10分钟平均风速风速，公里/小时
		String winDAvg10mi = nowObject.getString("wind_deg");//10分钟平均风向(角度)	度
		
		System.out.println(prs);
		System.out.println(prsSea);
		System.out.println(prsMax);
		System.out.println(prsMin);
		System.out.println(tem);
		System.out.println(temMax);
		System.out.println(temMin);
		System.out.println(rhu);
		System.out.println(rhuMin);
		System.out.println(vap);
		System.out.println(pre1h);
		System.out.println(winDInstMax);
		System.out.println(winSMax);
		System.out.println(winSInstMax);
		System.out.println(winDSMax);
		System.out.println(winSAvg10Mi);
		System.out.println(winDAvg10mi);
		
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}